/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Persistencia.JPAControllers;

import Logica.Core.MateriaPrima.Yerba;
import Persistencia.JPAControllers.exceptions.NonexistentEntityException;
import Persistencia.JPAControllers.exceptions.PreexistingEntityException;
import java.io.Serializable;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.Persistence;

/**
 *
 * @author DANIEL
 */
public class YerbaJpaController implements Serializable {

    public YerbaJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public YerbaJpaController() {
        emf = Persistence.createEntityManagerFactory("SecaderoPU");
    }

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Yerba yerba) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            em.persist(yerba);
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findYerba(yerba.getId()) != null) {
                throw new PreexistingEntityException("Yerba " + yerba + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Yerba yerba) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            yerba = em.merge(yerba);
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                int id = yerba.getId();
                if (findYerba(id) == null) {
                    throw new NonexistentEntityException("The yerba with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(int id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Yerba yerba;
            try {
                yerba = em.getReference(Yerba.class, id);
                yerba.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The yerba with id " + id + " no longer exists.", enfe);
            }
            em.remove(yerba);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Yerba> findYerbaEntities() {
        return findYerbaEntities(true, -1, -1);
    }

    public List<Yerba> findYerbaEntities(int maxResults, int firstResult) {
        return findYerbaEntities(false, maxResults, firstResult);
    }

    private List<Yerba> findYerbaEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Yerba as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Yerba findYerba(int id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Yerba.class, id);
        } finally {
            em.close();
        }
    }

    public int getYerbaCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Yerba as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
}
